module.exports = class
{
    constructor(step, grade)
    {
        this.grade = grade;
        this.step = step;
        this.stepType = '';
        this.awardCount = 0;
        this.awardItems = [];
        this.gradeData = [];
        this.sortedGradeData = [];
        this.awardItemsCount = this._getAwartItemsCount();
        this.judgerCount = this._getJudgerCount();
        this.judgers = [];//裁判

        this.allJudger = [];//所有的judger
        this.hasGradeJudger = [];//已经打分的评委
        this.nonGradeJudger = [];//没有打分的评委
        this.initJudger();
    }


    //获取处理好的数据
    list()
    {
        //获得比赛结果
        this._getGradeList();
        //处理获奖还是淘汰
        return {
            grade: this._getCPResult(),
            judgers: this.getJudgerArr(this.allJudger)
        };

    }

    _getCPResult()
    {
        //处理晋级还是颁奖
        this._getAwardCondition();
        this._sortGradeData();
        let gradeList = [];
        let data = this.sortedGradeData;
        if (this.stepType == '晋级')
        {
            //排序,并获得前五
            for (let i in data)
            {
                let nowGrade = data[i];
                if (i < this.awardCount)
                {
                    nowGrade.Res = '晋级';
                } else
                {
                    nowGrade.Res = '淘汰';
                }
                gradeList.push(nowGrade);
            }
            return gradeList;

        } else if (this.stepType == '颁奖')
        {
            //获得几等奖处理
            let nowIndex = 0;
            for (let i = 0; i < this.awardItemsCount && nowIndex < this.sortedGradeData.length; i++)
            {
                for (let j = 0; j < this.awardItems[i].Num && nowIndex < this.sortedGradeData.length; j++)
                {
                    let nowGrade = data[nowIndex];
                    nowGrade.Res = this.awardItems[i].Title;
                    nowIndex++;
                    gradeList.push(nowGrade);
                }
            }
            for (nowIndex; nowIndex < this.sortedGradeData.length && nowIndex < this.sortedGradeData.length; nowIndex++)
            {
                let nowGrade = data[nowIndex];
                nowGrade.Res = '淘汰';
                gradeList.push(nowGrade)
            }

            return gradeList;

        }

    }

    _getAwartItemsCount()
    {
        return this.step.stepsaward.length ? this.step.stepsaward.length : 0;
    }

    _sortGradeData()
    {
        // let res = [];
        let grades = this.gradeData;
        /* for (let index = 0; index < grades.length; index++)
         {
             let now = grades[index];
             let nowGrade = now.TotalGrade;
             for (let j = index + 1; j < grades.length; j++)
             {
                 if (nowGrade < grades[j].TotalGrade)
                 {
                     let t = now;
                     now = grades[j];
                     grades[j] = t;
                 }
             }
             // res.push(now);

         }*/
        grades.sort(function (first, second)
        {
            return second.TotalGrade - first.TotalGrade;
        });
        this.sortedGradeData = grades;
    }


    _getJudgerCount()
    {
        return this.step.competitionstepstojudger.length;
    }

    _getAwardCondition()
    {
        let steps = this.step;
        if (steps.Type == '晋级')
        {
            //获取颁奖人数
            this.stepType = steps.Type;
            this.awardCount = steps.QualifiedPerson;
        } else if (steps.Type == '颁奖')
        {
            this.stepType = steps.Type;
            this.awardItems = [];
            this._getStepsAwardItems();
            //处理晋级
        } else
        {
            return;
        }

    }

    _getStepsAwardItems()
    {
        let totalCount = 0;
        let items = this.step.stepsaward;
        for (let index in items)
        {
            let award = items[index];
            let awardObj = {}
            awardObj.Title = award.Title;
            awardObj.Num = award.Num;
            totalCount += award.Num;
            this.awardItems.push(awardObj);
        }
        this.awardCount = totalCount;
    }

    _getGradeList()
    {
        let grade = this.grade;
        let gradeArr = [];
        for (let index in grade)
        {
            let item = grade[index];
            let hasGradeJr = this.getHasGrade(item);//已经打分的裁判
            let allJr = this.getAllJr();
            let nonGradeJr = this.getNonGrade(allJr, hasGradeJr);

            let oneGrade = {};
            oneGrade.Title = item.works.Title;
            oneGrade.Auther = item.works.Auther;
            oneGrade.TotalGrade = 0;
            oneGrade.Grade = [];

            for (let i = 0; i < allJr.length; i++)
            {
                let val = allJr[i];
                let gradeItem = item.grade[i];
                let tempGrade = {};
                if (!think.isEmpty(gradeItem) && think.in_array(hasGradeJr, gradeItem['FK_JudgerGuid']))
                {
                    //该评委存在
                    tempGrade.grade = parseInt(gradeItem.Grade);
                    tempGrade.jrguid = gradeItem.FK_JudgerGuid;
                    oneGrade.TotalGrade += parseInt(tempGrade.grade);//计算总分

                } else
                {
                    tempGrade.grade = null;
                    tempGrade.jrguid = val;
                }

                oneGrade.Grade.push(tempGrade);
            }


            // for (let index = 0; index < this.judgerCount; index++)
            // {
            //     let gradeItem = item.grade[index];
            //     let tempGrade = {};
            //     if (gradeItem)
            //     {
            //         tempGrade.grade = parseInt(gradeItem.Grade) > 0 ? gradeItem.Grade : 0;
            //         tempGrade.jrguid = gradeItem.FK_JudgerGuid;
            //         this.judgers.push(gradeItem.FK_JudgerGuid);
            //         oneGrade.TotalGrade += parseInt(tempGrade.grade);//计算总分
            //     }
            //
            //     tempGrade.grade > 0 ? '' : tempGrade.grade = -1;
            //     oneGrade.Grade.push(tempGrade);
            //
            // }


            // hasGradeJr.forEach(function (val)
            // {
            //
            // });
            gradeArr.push(oneGrade);
        }
        this.gradeData = gradeArr;

    }

    initJudger()
    {
        this.allJudger = this.step.competitionstepstojudger;
    }


    getHasGrade(item)
    {
        let hasgradeguids = [];
        item.grade.forEach(function (val)
        {
            hasgradeguids.push(val.FK_JudgerGuid);
        })
        return hasgradeguids;
    }

    getNonGrade(allJr, hasGradeJr)
    {
        let temp = [];
        allJr.forEach(function (val)
        {
            if (!think.in_array(hasGradeJr, val)) temp.push(val)
        });
        return temp;
    }


    getAllJr()
    {
        let allJr = [];
        this.allJudger.forEach(function (val)
        {
            allJr.push(val.FK_JudgerGuid);
        });
        return allJr;
    }

    getJudgerArr(allJudger)
    {
        let t=[];
        allJudger.forEach(function (val)
        {
            t.push(val['FK_JudgerGuid']);
        })
        return t;
    }
};



